﻿
using Castle.Core.Internal;
using Coldairarrow.Business.Interface;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.Interface;
using Coldairarrow.Util;
using Newtonsoft.Json.Linq;
using OfficeOpenXml.Drawing.Slicer.Style;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.Tracing;
using System.Drawing;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static AutoMapper.Internal.CollectionMapperExpressionFactory;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace Coldairarrow.Business.Interface
{
    public class OL_Rep_OrgInventoryInterfaceBusiness : Interface_BaseBusiness, IJob_Business, ITransientDependency
    {
        public List<OL_FilterDTO> Filters;
        public OL_Rep_OrgInventoryInterfaceBusiness()
        {
            this.InterfaceName = "集团库存明细";
            //获取字段的映射关系
            tableField = GetMapField("editRow");
            //索引字段
            Keys = GetKeys("editRow");
            indexFields.Add(Keys.Where(X => X.Type == "K").First().Field);
            //查询过滤条件
            Filters = new List<OL_FilterDTO>();


            //查询执行前的事件
            BeforeQueryEvent += InterfaceBusiness_BeforeQueryEvent;
            //查询执行中的事件
            OnQueryEvent += InterfaceBusiness_OnQueryEvent;
            //执行非查询请求中的事件
            OnNoQueryEvent += InterfaceBusiness_OnNoQueryEvent;

        }

        /// <summary>
        /// 执行非查询请求中时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="noQueryEventArgs"></param>
        private void InterfaceBusiness_OnNoQueryEvent(object sender, NoQueryEventArgs noQueryEventArgs)
        {
            //API请求是否成功
            noQueryEventArgs.Success = noQueryEventArgs.Result["success"].ToString().ToBool();
        }

        /// <summary>
        /// 查询执行前的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="queryEventArgs"></param>
        private void InterfaceBusiness_BeforeQueryEvent(object sender, QueryEventArgs queryEventArgs)
        {
            //执行查询前添加过滤条件
            if (queryEventArgs.Page.Filters.IsNullOrEmpty() && !Filters.IsNullOrEmpty())
            {
                queryEventArgs.Page.Filters = Filters;
            }

        }

        /// <summary>
        /// 查询执行中的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="queryEventArgs"></param>
        private void InterfaceBusiness_OnQueryEvent(object sender, QueryEventArgs queryEventArgs)
        {
            //是否查询完成
            if (queryEventArgs.Result["data"]["rows"].ToList().Count == 0)
            {
                queryEventArgs.Page.Complete = true;
            }
            //向总行中添加新的行
            queryEventArgs.Rows.AddRange(queryEventArgs.Result["data"]["rows"].ToList());
            //获取总页数
            queryEventArgs.Page.Total = queryEventArgs.Result["data"]["total"].ToString().ToInt();
            //获取查询成功标志
            queryEventArgs.Success = queryEventArgs.Result["success"].ToString().ToBool();
        }


        /// <summary>
        /// 执行增删改查业务
        /// </summary>
        public async void ExecuteInterface()
        {

            try
            {
                //数据库连接
                ConString = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
                SqlDb = (SqlServerHelper)DbHelperFactory.GetDbHelper(EFCore.Sharding.DatabaseType.SqlServer, ConString);

                //获取此接口的所有API信息
                this.interfaceOption = SqlDb.GetListBySql<Base_InterfaceOption>(@$"SELECT * FROM  Base_InterfaceOption(NOLOCK) WHERE Name='{InterfaceName}' ");
                Base_InterfaceOption queryOption = this.interfaceOption.Where(o => o.OperateType == "getFilterRows").First();//查询API配置
                Base_InterfaceOption insertOption = this.interfaceOption.Where(o => o.OperateType == "addRow").First();//新增API配置
                Base_InterfaceOption upDateOption = this.interfaceOption.Where(o => o.OperateType == "editRow").First();//更行API配置
                Base_InterfaceOption deleteOption = this.interfaceOption.Where(o => o.OperateType == "deleteRow").First();//删除API配置

                var hour = DateTime.Now.Hour;
                var min = DateTime.Now.Minute;
                //立即更新库存用于记录当日的期末数据
                if (hour == 23 && (48 < min && min < 52))
                {

                    //更新FBA库存
                    FBAWarehouseInterfaceBusiness fBAWarehouseInterfaceBusiness = new FBAWarehouseInterfaceBusiness();
                    await fBAWarehouseInterfaceBusiness.ExecuteInterface("");
                    //更新易仓库存
                    ECInventoryBatchInterfaceBusiness eCInventoryBatchInterfaceBusiness = new ECInventoryBatchInterfaceBusiness();
                    await eCInventoryBatchInterfaceBusiness.ExecuteInterface("");
                }

                List<Rep_OrgInventory> stockLsit = new List<Rep_OrgInventory>();
                //众包库存
                //List<Rep_OrgInventory> PacklinInv = GetPacklinInv();
                //stockLsit.AddRange(PacklinInv);
                //九方库存
                List<Rep_OrgInventory> JiuFangInv = GetJiuFangInv();
                stockLsit.AddRange(JiuFangInv);
                //金蝶库存
                List<Rep_OrgInventory> KingDeeInv = GetKingDeeInv();
                stockLsit.AddRange(KingDeeInv);
                //摩台库存
                List<Rep_OrgInventory> KingDeeInv_MT = GetKingDeeInv_MT();
                stockLsit.AddRange(KingDeeInv_MT);
                //神匠库存
                List<Rep_OrgInventory> KingDeeInv_SJ = GetKingDeeInv_SJ();
                stockLsit.AddRange(KingDeeInv_SJ);
                //FBA库存
                List<Rep_OrgInventory> FBAInv = GetFBAInv();
                stockLsit.AddRange(FBAInv);
                //乐歌库存
                List<Rep_OrgInventory> LeGeInv = GetLeGeInv();
                stockLsit.AddRange(LeGeInv);
                //易仓库存
                List<Rep_OrgInventory> EcCangInv = GetEcCangInv();
                stockLsit.AddRange(EcCangInv);
                //俄罗斯
                List<Rep_OrgInventory> OneCInv = GetOneCInv();
                stockLsit.AddRange(OneCInv);
                //OZON
                List<Rep_OrgInventory> OZONInv = GetOzonInv();
                stockLsit.AddRange(OZONInv);
                //Yandex
                List<Rep_OrgInventory> YandexInv = GetYandexInv();
                stockLsit.AddRange(YandexInv);
                //Wildberries
                List<Rep_OrgInventory> WildberriesInv = GetWildberriesInv();
                stockLsit.AddRange(WildberriesInv);
                //RE
                List<Rep_OrgInventory> REInv = GetREInv();
                stockLsit.AddRange(REInv);

                List<Rep_OrgInventory> sourceData = new List<Rep_OrgInventory>();
                stockLsit.GroupBy(x => x.ID).ForEach(x =>
                {
                    Rep_OrgInventory stock = x.First().DeepClone();
                    stock.Cost = x.Sum(y => y.Cost);
                    stock.InInvQty = x.Sum(y => y.InInvQty);
                    stock.SaleQty = x.Sum(y => y.SaleQty);
                    stock.BadQty = x.Sum(y => y.BadQty);
                    stock.BadQty = x.Sum(y => y.BadQty);
                    stock.InTransitQty = x.Sum(y => y.InTransitQty);
                    stock.ReadyOutQty = x.Sum(y => y.ReadyOutQty);
                    stock.RebalanceQty = x.Sum(y => y.RebalanceQty);
                    stock.LockQty = x.Sum(y => y.LockQty);
                    stock.Past30IntStock = x.Sum(y => y.Past30IntStock);
                    stock.Past30OuttStock = x.Sum(y => y.Past30OuttStock);
                    stock.HighAge = x.Sum(y => y.HighAge);
                    stock.Age0_30 = x.Sum(y => y.Age0_30);
                    stock.Age0_90 = x.Sum(y => y.Age0_90);
                    stock.Age31_60 = x.Sum(y => y.Age31_60);
                    stock.Age61_90 = x.Sum(y => y.Age61_90);
                    stock.Age91_180 = x.Sum(y => y.Age91_180);
                    stock.Age181_270 = x.Sum(y => y.Age181_270);
                    stock.Age271_365 = x.Sum(y => y.Age271_365);
                    stock.Age_GreateThan365 = x.Sum(y => y.Age_GreateThan365);
                    stock.Age_GreateThan181 = x.Sum(y => y.Age_GreateThan181);
                    sourceData.Add(stock);

                });

                //库龄去除除可销数量外的数量
                SetInvAge(sourceData);






                //获取目标数据
                Filters.Clear();
                Filters.Add(
                //13:>,14>=15:<,16<=
                new OL_FilterDTO() { ControlId = "", DataType = 15, SpliceType = 1, FilterType = 14, Value = "" }//FDATE字段过滤日开
            );
                List<Rep_OrgInventory> targetData = await QueryAsync<Rep_OrgInventory>(queryOption, tableField, SqlDb);
                var ww = targetData.GroupBy(x => x.ID).Where(x => x.Count() > 1);
                if (this.State > 0 && sourceData.Count > 0)
                {
                    //更新
                    #region
                    List<Rep_OrgInventory> updataItem = sourceData.Intersect(targetData, new ComparerFromKeys<Rep_OrgInventory>(Keys, "RowId")).ToList();//获取交集(更新项)
                    List<OL_RowDTO> updataData = ConvertOL_Row(updataItem, tableField);//需要更新的数据集合
                    ExecuteNoQuery<OL_RowDTO>(updataData, upDateOption, SqlDb);//执行更新
                    #endregion

                    //新增
                    #region
                    List<Rep_OrgInventory> insertItem = sourceData.Except(targetData, new ComparerFromKey<Rep_OrgInventory>(indexFields.First(), "RowId")).ToList();//获取源数据对目标数据的差集(新增项)
                    List<OL_RowDTO> insertData = ConvertOL_Row(insertItem, tableField);//需要新增的数据集合
                    ExecuteNoQuery<OL_RowDTO>(insertData, insertOption, SqlDb);//执行插入行
                    #endregion

                    //删除
                    #region
                    List<Rep_OrgInventory> deleteItem = targetData.Except(sourceData, new ComparerFromKey<Rep_OrgInventory>(indexFields.First(), "RowId")).ToList();//获取目标数对据源数据的差集(删除项)
                    List<Rep_OrgInventory> repeatItem = targetData.GroupBy(x => x.GetPropertyValue(indexFields.First())).Where(x => x.Count() > 1).Select(x => x.FirstOrDefault()).ToList();
                    //将重复的项也一起加入删除的列表
                    deleteItem.AddRange(repeatItem);
                    //筛选出删除列表中重复的项
                    var temp = deleteItem.GroupBy(x => x.GetPropertyValue(indexFields.First())).Where(x => x.Count() > 1).Select(x => x.FirstOrDefault()).ToList();
                    deleteItem.AddRange(temp);
                    List<OL_RowDTO> deleteData = ConvertOL_Row(deleteItem, tableField);//需要删除的数据集合
                    ExecuteNoQuery<OL_RowDTO>(deleteData, deleteOption, SqlDb);//执行删除行
                    #endregion

                    //同步数据库
                    SqlDBInterfaceBusiness sqlDBInterface = new SqlDBInterfaceBusiness(Keys);
                    sqlDBInterface.ExecuteInterface<Rep_OrgInventory>(sourceData, "SD_OL_OrgInventory");


                    //记录当日的期末库存
                    if (hour == 17 && (48 < min && min < 52))
                    {
                        //同步数据库
                        sqlDBInterface.ExecuteInterface<Rep_OrgInventory>(sourceData.Where(x => x.Area != "中国" && x.Area != "俄罗斯").ToList(), "SD_OL_OrgInventoryOfEnd", "WHERE ID=''");
                    }
                }
            }


            catch (Exception ex)
            {
                AddLog(SqlDb, ex.Message, InterfaceName, "erro", "");
            }
        }

        /// <summary>
        /// 库龄去除除可销数量外的数量
        /// </summary>
        /// <param name="invData"></param>
        public void SetInvAge(List<Rep_OrgInventory> invData)
        {


            //keyValues.Add("Age0_30", 0);
            //keyValues.Add("Age31_60", 0);
            //keyValues.Add("Age61_90", 0);
            //keyValues.Add("Age91_180", 0);
            //keyValues.Add("Age181_270", 0);
            //keyValues.Add("Age271_365", 0);
            //keyValues.Add("Age_GreateThan365", 0);
            foreach (var item1 in invData)
            {
                if (item1.WarehouseType.Contains("BGA") || item1.WarehouseType.Contains("ELK") || item1.WarehouseType.Contains("九方") || item1.WarehouseType.Contains("亚美达"))
                {
                    if (item1.WarehouseType.Contains("BGA") && item1.PrdCode == "1.01.24.1172")
                    {
                        var WW = "";

                    }
                    Dictionary<string, decimal> keyValues = new Dictionary<string, decimal>();
                    keyValues.Add("Age0_30", (decimal)item1.GetPropertyValue("Age0_30"));
                    keyValues.Add("Age31_60", (decimal)item1.GetPropertyValue("Age31_60"));
                    keyValues.Add("Age61_90", (decimal)item1.GetPropertyValue("Age61_90"));
                    keyValues.Add("Age91_180", (decimal)item1.GetPropertyValue("Age91_180"));
                    keyValues.Add("Age181_270", (decimal)item1.GetPropertyValue("Age181_270"));
                    keyValues.Add("Age271_365", (decimal)item1.GetPropertyValue("Age271_365"));
                    keyValues.Add("Age_GreateThan365", (decimal)item1.GetPropertyValue("Age_GreateThan365"));
                    keyValues = keyValues.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
                    var ageCount = keyValues.Sum(x => x.Value);//获取库龄数量的求和
                    var count = ageCount - item1.SaleQty;//获取库龄-可销的差值
                    if (count > 0)
                    {
                        foreach (var item2 in keyValues)
                        {
                            //如果差值大于0则需要从库龄中最大
                            if (item2.Value > 0 && count > 0)
                            {
                                if (count > item2.Value)//如果差值大于库龄数量,库龄=0，剩余差值=差值-库龄数量
                                {
                                    keyValues[item2.Key] = 0;
                                    count = count - item2.Value;
                                }
                                else//如果小于，剩余库龄=库龄-差值
                                {
                                    keyValues[item2.Key] = item2.Value - count;
                                    count = 0;
                                }
                            }
                        }
                        item1.Age0_30 = keyValues["Age0_30"];
                        item1.Age31_60 = keyValues["Age31_60"];
                        item1.Age61_90 = keyValues["Age61_90"];
                        item1.Age91_180 = keyValues["Age91_180"];
                        item1.Age181_270 = keyValues["Age181_270"];
                        item1.Age271_365 = keyValues["Age271_365"];
                        item1.Age_GreateThan365 = keyValues["Age_GreateThan365"];
                    }


                }
            }
        }


        /// <summary>
        /// 将源数据根据字典映射关系转换成OL_RowDTO
        /// </summary>
        /// <typeparam name="T">源数据</typeparam>
        /// <param name="sourceData">源数据</param>
        /// <param name="map">Key为目标字段名称，Value为源字段名称</param>
        /// <returns></returns>
        public List<OL_RowDTO> ConvertOL_Row<T>(List<T> sourceData, Dictionary<string, string> map)
        {
            List<OL_RowDTO> list = new List<OL_RowDTO>();
            object rowId;
            foreach (var item in sourceData)
            {
                rowId = item.GetPropertyValue("RowId");
                //需要新增的数据
                OL_RowDTO row = new OL_RowDTO() { RowId = rowId.IsNullOrEmpty() ? null : rowId.ToString(), Controls = new List<Control>() };
                foreach (var c in map)
                {
                    //获取字段的键值做判断
                    var field = Keys.Where(x => x.TField == c.Key).ToList();
                    string isKey = "";
                    if (field.Count > 0) isKey = field.First().Type;
                    //判断对象的字段是否存在
                    if (item.ContainsProperty(c.Value) && isKey != "N")
                    {
                        //存在则添加数据
                        row.Controls.Add(new Control() { ControlId = c.Key, Value = item.GetPropertyValue(c.Value) });
                    }
                }
                list.Add(row);
            }
            return list;
        }


        public List<Rep_OrgInventory> GetKingDeeInv()
        {
            List<Rep_OrgInventory> invList = new List<Rep_OrgInventory>();
            #region 查询数据源
            //金蝶账龄表
            this.SqlDb._conStrings[0] = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
            List<V_STK_INVENTORYPRICE> StocsList = this.SqlDb.GetListBySql<V_STK_INVENTORYPRICE>(@$"SELECT * FROM SD_KD_V_STK_INVENTORYPRICE WHERE (FYTPE != '模具' and FYTPE != '虚拟件'and FYTPE != '服务') and (FSTOCKNAME not like '%模具%')");

            foreach (var item1 in StocsList)
            {
                Rep_OrgInventory orgInv = new Rep_OrgInventory();
                orgInv.PrdCode = item1.FNUMBER;
                orgInv.PrdName = item1.FNAME + "/规格:" + item1.FSPECIFICATION;
                orgInv.StorageOrg = item1.FORGNAME;
                orgInv.CargoOrg = item1.FORGNAME;
                orgInv.Area = "中国";
                if (item1.FSTOCKNAME.Contains("A"))
                {
                    orgInv.Warehouse = item1.FSTOCKNAME;
                    orgInv.WarehouseType = "一厂";
                }
                else if (item1.FSTOCKNAME.Contains("B"))
                {
                    orgInv.Warehouse = item1.FSTOCKNAME;
                    orgInv.WarehouseType = "二厂";
                }
                else
                {
                    orgInv.Warehouse = item1.FSTOCKNAME;
                    orgInv.WarehouseType = "力科";

                }
                orgInv.Category = item1.FYTPE;
                orgInv.InInvQty = item1.FSUMEQTY;
                orgInv.SaleQty = item1.FSUMEQTY;
                orgInv.BadQty = 0;
                orgInv.Cost = item1.FSUMPRICE;
                orgInv.Past30IntStock = 0;
                orgInv.Past30OuttStock = 0;
                orgInv.LockQty = 0;
                orgInv.InTransitQty = 0;
                orgInv.HighAge = item1.FBASEQTY_181to270 + item1.FBASEQTY_271to365 + item1.FBASEQTY_365to;
                orgInv.Age0_30 = item1.FBASEQTY_0to15 + item1.FBASEQTY_15to30;
                orgInv.Age31_60 = item1.FBASEQTY_30to60;
                orgInv.Age61_90 = item1.FBASEQTY_61to67 + item1.FBASEQTY_68to90;
                orgInv.Age91_180 = item1.FBASEQTY_90to120 + item1.FBASEQTY_120to180;
                orgInv.Age_GreateThan181 = item1.FBASEQTY_180to360 + item1.FBASEQTY_360to;

                orgInv.Age181_270 = item1.FBASEQTY_181to270;
                orgInv.Age271_365 = item1.FBASEQTY_271to365;
                orgInv.Age_GreateThan365 = item1.FBASEQTY_365to;
                orgInv.ID = orgInv.PrdCode + orgInv.Area + orgInv.Warehouse;
                invList.Add(orgInv);
            }
            #endregion
            return invList;
        }


        public List<Rep_OrgInventory> GetKingDeeInv_MT()
        {
            List<Rep_OrgInventory> invList = new List<Rep_OrgInventory>();
            #region 查询数据源
            //摩台库存表
            this.SqlDb._conStrings[0] = JsonSettingHelper.GetSetting("Database:BaseDb:MTConnectionString");
            List<v_stk_inventory> StocsList = this.SqlDb.GetListBySql<v_stk_inventory>(@$"SELECT * FROM V_WW_STK_INVENTORY");

            foreach (var item1 in StocsList)
            {
                Rep_OrgInventory orgInv = new Rep_OrgInventory();
                orgInv.PrdCode = item1.item_code;
                orgInv.PrdName = item1.item_name + "/规格:" + item1.specs; ;
                orgInv.StorageOrg = item1.fstockorgname;
                orgInv.CargoOrg = item1.fstockorgname;
                orgInv.Area = "中国";
                orgInv.Warehouse = item1.warehouse_name;
                orgInv.WarehouseType = "摩台";
                //orgInv.Category = item1.FYTPE;
                orgInv.InInvQty = Convert.ToDecimal(item1.basic_qty);
                orgInv.SaleQty = Convert.ToDecimal(item1.basic_qty);
                orgInv.BadQty = 0;
                //orgInv.Cost = item1.FSUMPRICE;
                orgInv.Past30IntStock = 0;
                orgInv.Past30OuttStock = 0;
                orgInv.LockQty = 0;
                orgInv.InTransitQty = 0;
                //orgInv.HighAge = 0;
                //orgInv.Age0_30 = item1.FBASEQTY_0to15 + item1.FBASEQTY_15to30;
                //orgInv.Age31_60 = item1.FBASEQTY_30to60;
                //orgInv.Age61_90 = item1.FBASEQTY_61to67 + item1.FBASEQTY_68to90;
                //orgInv.Age91_180 = item1.FBASEQTY_90to120 + item1.FBASEQTY_120to180;
                //orgInv.Age_GreateThan181 = item1.FBASEQTY_180to360 + item1.FBASEQTY_360to;
                orgInv.ID = orgInv.PrdCode + orgInv.Area + orgInv.Warehouse;
                invList.Add(orgInv);
            }
            #endregion
            return invList;
        }

        public List<Rep_OrgInventory> GetKingDeeInv_SJ()
        {
            List<Rep_OrgInventory> invList = new List<Rep_OrgInventory>();
            #region 查询数据源
            //神匠库存表
            this.SqlDb._conStrings[0] = JsonSettingHelper.GetSetting("Database:BaseDb:SJConnectionString");
            List<v_stk_inventory> StocsList = this.SqlDb.GetListBySql<v_stk_inventory>(@$"SELECT * FROM V_WW_STK_INVENTORY");
            foreach (var item1 in StocsList)
            {
                Rep_OrgInventory orgInv = new Rep_OrgInventory();
                orgInv.PrdCode = item1.item_code;
                orgInv.PrdName = item1.item_name + "/规格:" + item1.specs; ;
                orgInv.StorageOrg = item1.fstockorgname;
                orgInv.CargoOrg = item1.fstockorgname;
                orgInv.Area = "中国";
                orgInv.Warehouse = item1.warehouse_name;
                orgInv.WarehouseType = "神匠";
                //orgInv.Category = item1.FYTPE;
                orgInv.InInvQty = Convert.ToDecimal(item1.basic_qty);
                orgInv.SaleQty = Convert.ToDecimal(item1.basic_qty);
                orgInv.BadQty = 0;
                //orgInv.Cost = item1.FSUMPRICE;
                orgInv.Past30IntStock = 0;
                orgInv.Past30OuttStock = 0;
                orgInv.LockQty = 0;
                orgInv.InTransitQty = 0;
                //orgInv.HighAge = 0;
                //orgInv.Age0_30 = item1.FBASEQTY_0to15 + item1.FBASEQTY_15to30;
                //orgInv.Age31_60 = item1.FBASEQTY_30to60;
                //orgInv.Age61_90 = item1.FBASEQTY_61to67 + item1.FBASEQTY_68to90;
                //orgInv.Age91_180 = item1.FBASEQTY_90to120 + item1.FBASEQTY_120to180;
                //orgInv.Age_GreateThan181 = item1.FBASEQTY_180to360 + item1.FBASEQTY_360to;
                orgInv.ID = orgInv.PrdCode + orgInv.Area + orgInv.Warehouse;
                invList.Add(orgInv);
            }
            #endregion
            return invList;
        }

        public List<Rep_OrgInventory> GetFBAInv()
        {
            List<Rep_OrgInventory> invList = new List<Rep_OrgInventory>();

            //获取店铺
            OL_ShopInterfaceBusiness OL_Shop = new OL_ShopInterfaceBusiness();
            OL_Shop.Filters.Add(
        new OL_FilterDTO() { ControlId = "", DataType = 29, SpliceType = 1, FilterType = 8, Value = "" }
            //new OL_FilterDTO() { ControlId = "661d1c34387bc49b7a55d06e", DataType = 15, SpliceType = 1, FilterType = 14, Value = "2024-11-24" }
            );
            List<OL_Shop> shopList = OL_Shop.Query<OL_Shop>(OL_Shop.queryOption, OL_Shop.tableField, OL_Shop.SqlDb);


            #region 查询数据源
            //FBA/FBM账龄表
            this.SqlDb._conStrings[0] = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
            List<FBAWarehouse> StocsList = this.SqlDb.GetListBySql<FBAWarehouse>(@$"SELECT * FROM  SD_LX_FBAWarehouse");
            //fba库存
            foreach (var item1 in StocsList.Where(x => x.fulfillment_channel.Contains("AMAZON") || x.afn_fulfillable_quantity > 0))
            {
                Rep_OrgInventory orgInv = new Rep_OrgInventory();
                orgInv.PrdCode = item1.seller_sku;
                orgInv.PrdName = "";
                orgInv.StorageOrg = "";
                orgInv.CargoOrg = "";
                var stock = shopList.Where(x => item1.name.Contains(x.Name));
                if (!stock.IsNullOrEmpty())
                {
                    orgInv.Area = stock.First().Country;
                }
                else if (item1.name.Contains("美"))
                {
                    orgInv.Area = "美国";
                }
                else if (item1.name.Contains("牙"))
                {
                    orgInv.Area = "西班牙";
                }
                else
                {
                    orgInv.Area = "";

                }
                orgInv.Warehouse = item1.name;
                orgInv.WarehouseType = "亚马逊FBA";
                orgInv.Category = "";
                orgInv.InInvQty = item1.available_total;
                orgInv.SaleQty = item1.afn_fulfillable_quantity;
                orgInv.ReadyOutQty = item1.reserved_customerorders;
                orgInv.RebalanceQty = item1.reserved_fc_transfers + item1.reserved_fc_processing;
                orgInv.InStorageQty = item1.afn_inbound_receiving_quantity;
                orgInv.BadQty = 0;
                orgInv.Cost = 0;
                orgInv.Past30IntStock = 0;
                orgInv.Past30OuttStock = 0;
                orgInv.LockQty = 0;
                orgInv.InTransitQty = item1.afn_inbound_shipped_quantity;
                orgInv.HighAge = item1.inv_age_181_to_270_days + item1.inv_age_271_to_365_days + item1.inv_age_365_plus_days;
                orgInv.Age0_30 = item1.inv_age_0_to_30_days;
                orgInv.Age31_60 = item1.inv_age_31_to_60_days;
                orgInv.Age61_90 = item1.inv_age_61_to_90_days;
                orgInv.Age91_180 = item1.inv_age_91_to_180_days;
                orgInv.Age_GreateThan181 = item1.inv_age_181_to_270_days + item1.inv_age_271_to_365_days + item1.inv_age_365_plus_days;

                orgInv.Age181_270 = item1.inv_age_181_to_270_days;
                orgInv.Age271_365 = item1.inv_age_271_to_365_days;
                orgInv.Age_GreateThan365 = item1.inv_age_365_plus_days;
                orgInv.ID = orgInv.PrdCode + orgInv.Area + orgInv.Warehouse + orgInv.WarehouseType;
                invList.Add(orgInv);
            }
            //fbm库存
            foreach (var item1 in StocsList.Where(x => x.fulfillment_channel == "DEFAULT" || x.quantity > 0))
            {
                Rep_OrgInventory orgInv = new Rep_OrgInventory();
                orgInv.PrdCode = item1.seller_sku;
                orgInv.PrdName = "";
                orgInv.StorageOrg = "";
                orgInv.CargoOrg = "";
                var stock = shopList.Where(x => item1.name.Contains(x.Name));
                if (!stock.IsNullOrEmpty())
                {
                    orgInv.Area = stock.First().Country;
                }
                else if (item1.name.Contains("美"))
                {
                    orgInv.Area = "美国";
                }
                else if (item1.name.Contains("牙"))
                {
                    orgInv.Area = "西班牙";
                }
                else
                {
                    orgInv.Area = "";

                }
                orgInv.Warehouse = item1.name;
                orgInv.WarehouseType = "亚马逊FBM";
                orgInv.Category = "";
                orgInv.InInvQty = item1.available_total;
                orgInv.SaleQty = item1.quantity;
                orgInv.ReadyOutQty = item1.reserved_customerorders;
                orgInv.RebalanceQty = item1.reserved_fc_transfers + item1.reserved_fc_processing;
                orgInv.InStorageQty = item1.afn_inbound_receiving_quantity;
                orgInv.BadQty = 0;
                orgInv.Cost = 0;
                orgInv.Past30IntStock = 0;
                orgInv.Past30OuttStock = 0;
                orgInv.LockQty = 0;
                orgInv.InTransitQty = item1.afn_inbound_shipped_quantity + item1.afn_inbound_receiving_quantity;
                orgInv.HighAge = item1.inv_age_181_to_270_days + item1.inv_age_271_to_365_days + item1.inv_age_365_plus_days;
                orgInv.Age0_30 = item1.inv_age_0_to_30_days;
                orgInv.Age31_60 = item1.inv_age_31_to_60_days;
                orgInv.Age61_90 = item1.inv_age_61_to_90_days;
                orgInv.Age91_180 = item1.inv_age_91_to_180_days;
                orgInv.Age_GreateThan181 = item1.inv_age_181_to_270_days + item1.inv_age_271_to_365_days + item1.inv_age_365_plus_days;

                orgInv.Age181_270 = item1.inv_age_181_to_270_days;
                orgInv.Age271_365 = item1.inv_age_271_to_365_days;
                orgInv.Age_GreateThan365 = item1.inv_age_365_plus_days;

                orgInv.ID = orgInv.PrdCode + orgInv.Area + orgInv.Warehouse + orgInv.WarehouseType;
                invList.Add(orgInv);
            }


            #endregion
            return invList;
        }

        public List<Rep_OrgInventory> GetLeGeInv()
        {
            List<Rep_OrgInventory> invList = new List<Rep_OrgInventory>();
            #region 查询数据源
            //乐歌账龄表
            this.SqlDb._conStrings[0] = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
            List<LGInventory> StocsList = this.SqlDb.GetListBySql<LGInventory>(@$"SELECT * FROM  SD_LG_LGInventory WHERE goodsNum>0");
            foreach (var item1 in StocsList)
            {
                Rep_OrgInventory orgInv = new Rep_OrgInventory();
                orgInv.PrdCode = item1.goodsCode;
                orgInv.PrdName = "";
                orgInv.StorageOrg = "";
                orgInv.CargoOrg = "";
                orgInv.Area = "美国";
                orgInv.Warehouse = item1.warehouseCode;
                orgInv.WarehouseType = "乐歌三方仓";
                orgInv.Category = "";
                orgInv.InInvQty = item1.goodsNum;
                orgInv.SaleQty = item1.goodsNum;
                orgInv.BadQty = 0;
                orgInv.Cost = 0;
                orgInv.Past30IntStock = 0;
                orgInv.Past30OuttStock = 0;
                orgInv.LockQty = item1.blockedNum;
                orgInv.InTransitQty = item1.onWayNum;
                orgInv.HighAge = item1.age181_270 + item1.age271_365 + item1.age365_;
                //orgInv.Age0_30 = item1.inv_age_0_to_30_days;
                //orgInv.Age31_60 = item1.inv_age_31_to_60_days;
                orgInv.Age0_90 = item1.age0_90;
                orgInv.Age91_180 = item1.age91_180;
                orgInv.Age181_270 = item1.age181_270;
                orgInv.Age271_365 = item1.age271_365;
                orgInv.Age_GreateThan181 = item1.age181_270 + item1.age271_365 + item1.age365_;
                orgInv.Age_GreateThan365 = item1.age365_;
                orgInv.ID = orgInv.PrdCode + orgInv.Area + orgInv.Warehouse;
                invList.Add(orgInv);
            }

            #endregion
            return invList;
        }

        public List<Rep_OrgInventory> GetEcCangInv()
        {
            List<Rep_OrgInventory> invList = new List<Rep_OrgInventory>();
            #region 查询数据源
            //易仓账龄表
            this.SqlDb._conStrings[0] = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
            //获取不需要查询的仓库
            Base_ConfigParameter config = this.SqlDb.GetListBySql<Base_ConfigParameter>(@$"SELECT * FROM  Base_ConfigParameter WHERE Name='EC_LapseWarehouse'").First();
            var lapseWarehouse = config.Value.Split(",");
            StringBuilder stringBuilder = new StringBuilder();
            if (lapseWarehouse.Length > 0)
            {
                stringBuilder.Append(" WHERE ");
                for (int i = 0; i < lapseWarehouse.Length; i++)
                {
                    string s = $" warehouse_name not like '%{lapseWarehouse[i]}%' ";
                    if (i == lapseWarehouse.Length - 1) stringBuilder.Append(s);
                    else stringBuilder.Append(s + "and");
                }
            }
            List<ProInv> StocsList = this.SqlDb.GetListBySql<ProInv>(@$"SELECT * FROM  SD_EC_ProInv {stringBuilder.ToString()}");
            List<Ec_InventoryBatch> InventoryBatchList = this.SqlDb.GetListBySql<Ec_InventoryBatch>(@$"SELECT * FROM  SD_EC_InventoryBatch");
            //BGA头程单
            List<EC_Receiving> receivingList = this.SqlDb.GetListBySql<EC_Receiving>(@$"SELECT * FROM SD_EC_Receiving WHERE receiving_type='标准' AND (receiving_status='收货中' OR receiving_status='在途')");
            foreach (var item1 in StocsList)
            {
                Rep_OrgInventory orgInv = new Rep_OrgInventory();
                orgInv.PrdCode = item1.product_sku;
                orgInv.FNSKU = item1.FNSKU.IsNullOrEmpty() ? item1.product_sku : item1.FNSKU;
                orgInv.PrdName = item1.product_title;
                orgInv.StorageOrg = "";
                orgInv.CargoOrg = "";
                orgInv.Area = "美国";
                orgInv.Warehouse = item1.warehouse_name;
                if (item1.warehouse_name.Contains("ELK"))
                {
                    orgInv.WarehouseType = "ELK仓";
                }
                else if (item1.warehouse_name.Contains("LCE"))
                {
                    orgInv.WarehouseType = "LCE仓";
                }
                else if (item1.warehouse_name.Contains("橙联"))
                {
                    orgInv.WarehouseType = "橙联";
                }
                else if (item1.warehouse_name.Contains("BGA自建仓"))
                {
                    orgInv.WarehouseType = "BGA自建仓";
                }
                else if (item1.warehouse_name.Contains("亚美达"))
                {
                    orgInv.WarehouseType = "亚美达";
                }
                else if (item1.warehouse_name.Contains("九方"))
                {
                    orgInv.WarehouseType = "九方";
                }
                else
                {
                    orgInv.WarehouseType = item1.warehouse_name;
                }
                orgInv.Category = "";
                orgInv.InInvQty = item1.pi_in_used_qty;
                orgInv.SaleQty = item1.pi_sellable_qty;
                orgInv.ReadyOutQty = item1.pi_reserved_qty;
                orgInv.BadQty = item1.pi_unsellable_qty;
                orgInv.Cost = item1.inventory_cost;
                orgInv.Past30IntStock = 0;
                orgInv.Past30OuttStock = 0;
                orgInv.LockQty = 0;

                if (item1.warehouse_name.Contains("BGA自建仓"))
                {

                    var inTransitList = receivingList.Where(x => x.product_info_dif_receive_num != "0" && x.product_info_rd_status_string == "在途" && x.product_info_product_barcode == item1.product_sku && x.warehouse.Contains("BGA"));
                    orgInv.InTransitQty = !inTransitList.IsNullOrEmpty() && inTransitList.Count() > 0 ? inTransitList.Sum(x => x.product_info_dif_receive_num.ToInt()) : 0; //将在途的差异数量相加
                    var inStorageQty = receivingList.Where(x => x.product_info_dif_receive_num != "0" && x.product_info_rd_status_string == "收货中" && x.product_info_product_barcode == item1.product_sku && x.warehouse.Contains("BGA"));
                    orgInv.InStorageQty = !inStorageQty.IsNullOrEmpty() && inStorageQty.Count() > 0 ? inStorageQty.Sum(x => x.product_info_dif_receive_num.ToInt()) : 0; //将收货中的差异数量相加

                }
                else
                {
                    orgInv.InTransitQty = item1.pi_purchase_onway_qty + item1.pi_return_onway_qty;
                    orgInv.InStorageQty = item1.pi_pending_qty;
                }


                var temp = InventoryBatchList.Where(x => x.ageFromUpdate >= 0 && x.ageFromUpdate <= 30 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
                orgInv.Age0_30 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
                temp = InventoryBatchList.Where(x => x.ageFromUpdate >= 31 && x.ageFromUpdate <= 60 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
                orgInv.Age31_60 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
                temp = InventoryBatchList.Where(x => x.ageFromUpdate >= 61 && x.ageFromUpdate <= 90 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
                orgInv.Age61_90 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
                temp = InventoryBatchList.Where(x => x.ageFromUpdate >= 0 && x.ageFromUpdate <= 90 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
                orgInv.Age0_90 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
                temp = InventoryBatchList.Where(x => x.ageFromUpdate >= 91 && x.ageFromUpdate <= 180 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
                orgInv.Age91_180 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
                temp = InventoryBatchList.Where(x => x.ageFromUpdate >= 181 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
                orgInv.Age_GreateThan181 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
                orgInv.HighAge = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;

                temp = InventoryBatchList.Where(x => x.ageFromUpdate >= 181 && x.ageFromUpdate <= 270 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
                orgInv.Age181_270 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
                temp = InventoryBatchList.Where(x => x.ageFromUpdate >= 271 && x.ageFromUpdate <= 365 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
                orgInv.Age271_365 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
                temp = InventoryBatchList.Where(x => x.ageFromUpdate >= 365 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
                orgInv.Age_GreateThan365 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;

                orgInv.ID = orgInv.PrdCode + orgInv.Area + orgInv.Warehouse;
                invList.Add(orgInv);
            }


            //豹仓
            //var baoCang = StocsList.Where(x => x.warehouse_name.Contains("豹仓"));
            //foreach (var item1 in baoCang)
            //{
            //    Rep_OrgInventory orgInv = new Rep_OrgInventory();
            //    orgInv.PrdCode = item1.product_sku;
            //    orgInv.FNSKU = item1.FNSKU.IsNullOrEmpty() ? item1.product_sku : item1.FNSKU;
            //    orgInv.PrdName = item1.product_title;
            //    orgInv.StorageOrg = "";
            //    orgInv.CargoOrg = "";
            //    orgInv.Area = "美国";
            //    orgInv.Warehouse = item1.warehouse_name;
            //    orgInv.WarehouseType = "豹仓三方仓";
            //    orgInv.Category = "";
            //    orgInv.InInvQty = item1.pi_in_used_qty;
            //    orgInv.SaleQty = item1.pi_sellable_qty;
            //    orgInv.ReadyOutQty = item1.pi_reserved_qty;
            //    orgInv.BadQty = item1.pi_unsellable_qty;
            //    orgInv.Cost = item1.inventory_cost;
            //    orgInv.Past30IntStock = 0;
            //    orgInv.Past30OuttStock = 0;
            //    orgInv.LockQty = 0;
            //    orgInv.InTransitQty = item1.pi_purchase_onway_qty + item1.pi_return_onway_qty;
            //    orgInv.InStorageQty = item1.pi_pending_qty;
            //    var temp = InventoryBatchList.Where(x => x.age >= 0 && x.age <= 30 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age0_30 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 31 && x.age <= 60 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age31_60 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 0 && x.age <= 90 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age0_90 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 91 && x.age <= 180 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age91_180 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 181 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age_GreateThan181 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    orgInv.HighAge = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;

            //    temp = InventoryBatchList.Where(x => x.age >= 181 && x.age <= 270 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age181_270 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 271 && x.age <= 365 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age271_365 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 365 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age_GreateThan365 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    orgInv.ID = orgInv.PrdCode + orgInv.Area + orgInv.Warehouse;
            //    invList.Add(orgInv);
            //}

            ////橙联
            //var cengLian = StocsList.Where(x => x.warehouse_name.Contains("橙联"));
            //foreach (var item1 in cengLian)
            //{
            //    Rep_OrgInventory orgInv = new Rep_OrgInventory();
            //    orgInv.PrdCode = item1.product_sku;
            //    orgInv.FNSKU = item1.FNSKU.IsNullOrEmpty() ? item1.product_sku : item1.FNSKU;
            //    orgInv.PrdName = item1.product_title;
            //    orgInv.StorageOrg = "";
            //    orgInv.CargoOrg = "";
            //    orgInv.Area = "美国";
            //    orgInv.Warehouse = item1.warehouse_name;
            //    orgInv.WarehouseType = "橙联";
            //    orgInv.Category = "";
            //    orgInv.InInvQty = item1.pi_in_used_qty;
            //    orgInv.SaleQty = item1.pi_sellable_qty;
            //    orgInv.ReadyOutQty = item1.pi_reserved_qty;
            //    orgInv.BadQty = item1.pi_unsellable_qty;
            //    orgInv.Cost = item1.inventory_cost;
            //    orgInv.Past30IntStock = 0;
            //    orgInv.Past30OuttStock = 0;
            //    orgInv.LockQty = 0;
            //    orgInv.InTransitQty = item1.pi_purchase_onway_qty + item1.pi_return_onway_qty;
            //    orgInv.InStorageQty = item1.pi_pending_qty;
            //    var temp = InventoryBatchList.Where(x => x.age >= 0 && x.age <= 30 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age0_30 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 31 && x.age <= 60 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age31_60 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 0 && x.age <= 90 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age0_90 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 91 && x.age <= 180 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age91_180 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 181 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age_GreateThan181 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    orgInv.HighAge = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;

            //    temp = InventoryBatchList.Where(x => x.age >= 181 && x.age <= 270 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age181_270 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 271 && x.age <= 365 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age271_365 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 365 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age_GreateThan365 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    orgInv.ID = orgInv.PrdCode + orgInv.Area + orgInv.Warehouse;
            //    invList.Add(orgInv);
            //}

            ////BGA
            //var BGA = StocsList.Where(x => x.warehouse_name.Contains("BGA自建仓"));
            ////头程单
            //List<EC_Receiving> receivingList = this.SqlDb.GetListBySql<EC_Receiving>(@$"SELECT * FROM SD_EC_Receiving WHERE receiving_type='标准' AND (receiving_status='收货中' OR receiving_status='在途')");
            //foreach (var item1 in BGA)
            //{
            //    Rep_OrgInventory orgInv = new Rep_OrgInventory();
            //    orgInv.PrdCode = item1.product_sku;
            //    orgInv.FNSKU = item1.FNSKU.IsNullOrEmpty() ? item1.product_sku : item1.FNSKU;
            //    orgInv.PrdName = item1.product_title;
            //    orgInv.StorageOrg = "";
            //    orgInv.CargoOrg = "";
            //    orgInv.Area = "美国";
            //    orgInv.Warehouse = item1.warehouse_name;
            //    orgInv.WarehouseType = "BGA自建仓";
            //    orgInv.Category = "";
            //    orgInv.InInvQty = item1.pi_in_used_qty;
            //    orgInv.SaleQty = item1.pi_sellable_qty;
            //    orgInv.ReadyOutQty = item1.pi_reserved_qty;
            //    orgInv.BadQty = item1.pi_unsellable_qty;
            //    orgInv.Cost = item1.inventory_cost;
            //    orgInv.Past30IntStock = 0;
            //    orgInv.Past30OuttStock = 0;
            //    orgInv.LockQty = 0;
            //    var inTransitList = receivingList.Where(x => x.product_info_dif_receive_num != "0" && x.product_info_rd_status_string == "在途" && x.product_info_product_barcode == item1.product_sku && x.warehouse.Contains("BGA"));
            //    orgInv.InTransitQty = !inTransitList.IsNullOrEmpty() && inTransitList.Count() > 0 ? inTransitList.Sum(x => x.product_info_dif_receive_num.ToInt()) : 0; //将在途的差异数量相加
            //    var inStorageQty = receivingList.Where(x => x.product_info_dif_receive_num != "0" && x.product_info_rd_status_string == "收货中" && x.product_info_product_barcode == item1.product_sku && x.warehouse.Contains("BGA"));
            //    orgInv.InStorageQty = !inStorageQty.IsNullOrEmpty() && inStorageQty.Count() > 0 ? inStorageQty.Sum(x => x.product_info_dif_receive_num.ToInt()) : 0; //将收货中的差异数量相加

            //    var temp = InventoryBatchList.Where(x => x.age >= 0 && x.age <= 30 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age0_30 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 31 && x.age <= 60 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age31_60 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 0 && x.age <= 90 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age0_90 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 91 && x.age <= 180 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age91_180 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 181 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age_GreateThan181 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    orgInv.HighAge = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;

            //    temp = InventoryBatchList.Where(x => x.age >= 181 && x.age <= 270 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age181_270 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 271 && x.age <= 365 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age271_365 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 365 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age_GreateThan365 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;

            //    orgInv.ID = orgInv.PrdCode + orgInv.Area + orgInv.Warehouse;
            //    invList.Add(orgInv);
            //}



            ////内置仓库
            //var nz = StocsList.Where(x => x.warehouse_name.Contains("内置仓库"));
            //foreach (var item1 in nz)
            //{
            //    Rep_OrgInventory orgInv = new Rep_OrgInventory();
            //    orgInv.PrdCode = item1.product_sku;
            //    orgInv.FNSKU = item1.FNSKU.IsNullOrEmpty() ? item1.product_sku : item1.FNSKU;
            //    orgInv.PrdName = item1.product_title;
            //    orgInv.StorageOrg = "";
            //    orgInv.CargoOrg = "";
            //    orgInv.Area = "美国";
            //    orgInv.Warehouse = item1.warehouse_name;
            //    orgInv.WarehouseType = "内置仓库";
            //    orgInv.Category = "";
            //    orgInv.InInvQty = item1.pi_in_used_qty;
            //    orgInv.SaleQty = item1.pi_sellable_qty;
            //    orgInv.ReadyOutQty = item1.pi_reserved_qty;
            //    orgInv.BadQty = item1.pi_unsellable_qty;
            //    orgInv.Cost = item1.inventory_cost;
            //    orgInv.Past30IntStock = 0;
            //    orgInv.Past30OuttStock = 0;
            //    orgInv.LockQty = 0;
            //    orgInv.InTransitQty = item1.pi_purchase_onway_qty + item1.pi_return_onway_qty;
            //    orgInv.InStorageQty = item1.pi_pending_qty;
            //    var temp = InventoryBatchList.Where(x => x.age >= 0 && x.age <= 30 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age0_30 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 31 && x.age <= 60 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age31_60 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 0 && x.age <= 90 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age0_90 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 91 && x.age <= 180 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age91_180 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 181 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age_GreateThan181 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    orgInv.HighAge = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;

            //    temp = InventoryBatchList.Where(x => x.age >= 181 && x.age <= 270 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age181_270 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 271 && x.age <= 365 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age271_365 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    temp = InventoryBatchList.Where(x => x.age >= 365 && x.product_sku == item1.product_sku && x.warehouse_id == item1.warehouse_id);
            //    orgInv.Age_GreateThan365 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.ib_quantity) : 0;
            //    orgInv.ID = orgInv.PrdCode + orgInv.Area + orgInv.Warehouse;
            //    invList.Add(orgInv);
            //}
            #endregion
            return invList;
        }


        public List<Rep_OrgInventory> GetOneCInv()
        {
            List<Rep_OrgInventory> invList = new List<Rep_OrgInventory>();
            #region 查询数据源
            //1C库存表
            this.SqlDb._conStrings[0] = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
            List<Rep_1CInventory> StocsList = this.SqlDb.GetListBySql<Rep_1CInventory>(@$"SELECT * FROM  SD_1C_Rep_1CInventory");
            //OL仓库转换基础资料

            foreach (var item1 in StocsList)
            {
                Rep_OrgInventory orgInv = new Rep_OrgInventory();
                orgInv.PrdCode = item1.productNo;
                orgInv.PrdName = item1.productName;
                orgInv.StorageOrg = "";
                orgInv.CargoOrg = "";
                orgInv.Area = "俄罗斯";
                if (!item1.warehouseType.IsNullOrEmpty())
                {
                    orgInv.WarehouseType = item1.warehouseType;
                    orgInv.Warehouse = item1.warehouse;
                }
                else
                {
                    orgInv.WarehouseType = "俄罗斯其他仓";
                    orgInv.Warehouse = item1.warehouse;
                }

                orgInv.Category = "";
                orgInv.InInvQty = item1.qty;
                orgInv.SaleQty = item1.qty;
                orgInv.BadQty = 0;
                orgInv.Cost = 0;
                orgInv.Past30IntStock = 0;
                orgInv.Past30OuttStock = 0;
                orgInv.LockQty = 0;
                orgInv.InTransitQty = 0;
                //orgInv.HighAge = 0;
                //orgInv.Age0_30 = 0;
                //orgInv.Age31_60 = 0;
                //orgInv.Age0_90 = 0;
                //orgInv.Age91_180 = 0;
                //orgInv.Age_GreateThan181 = 0;
                orgInv.ID = (orgInv.PrdName + orgInv.Area + orgInv.Warehouse).ToMD5String();
                invList.Add(orgInv);
            }

            #endregion
            return invList;
        }

        public List<Rep_OrgInventory> GetJiuFangInv()
        {
            List<Rep_OrgInventory> invList = new List<Rep_OrgInventory>();
            #region 查询数据源
            //FBA账龄表
            this.SqlDb._conStrings[0] = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
            List<JF_SkuInv> StocsList = this.SqlDb.GetListBySql<JF_SkuInv>(@$"SELECT * FROM  SD_JF_SkuInv");
            List<JF_SkuBatchInv> batchInvList = this.SqlDb.GetListBySql<JF_SkuBatchInv>(@$"SELECT * FROM SD_JF_SkuBatchInv");


            foreach (var item1 in StocsList.GroupBy(x => x.skuCode + x.whCode))
            {
                var first = item1.First();
                Rep_OrgInventory orgInv = new Rep_OrgInventory();
                orgInv.PrdCode = first.skuCode;
                orgInv.PrdName = first.skuName;
                orgInv.StorageOrg = "";
                orgInv.CargoOrg = "";
                orgInv.Area = "美国";
                orgInv.Warehouse = first.whName;
                orgInv.WarehouseType = "九方仓";
                orgInv.Category = "";
                orgInv.InInvQty = item1.Sum(x => x.availableQty + x.waitOutQty + x.unavailableQty + x.returnBadQty + x.returnQty);
                orgInv.SaleQty = item1.Sum(x => x.availableQty);
                orgInv.ReadyOutQty = item1.Sum(x => x.waitOutQty);
                orgInv.BadQty = item1.Sum(x => x.unavailableQty + x.returnBadQty);
                orgInv.Cost = 0;
                orgInv.Past30IntStock = 0;
                orgInv.Past30OuttStock = 0;
                orgInv.LockQty = item1.Sum(x => x.occupyQty);
                orgInv.InTransitQty = item1.Sum(x => x.onWayQty);
                orgInv.InStorageQty = item1.Sum(x => x.receivingQty);

                var temp = batchInvList.Where(x => x.skuCode == first.skuCode && x.whCode == first.whCode && x.storageAge >= 0 && x.storageAge <= 30);
                orgInv.Age0_30 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.totalQty) : 0;
                temp = batchInvList.Where(x => x.skuCode == first.skuCode && x.whCode == first.whCode && x.storageAge >= 31 && x.storageAge <= 60);
                orgInv.Age31_60 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.totalQty) : 0;
                temp = batchInvList.Where(x => x.skuCode == first.skuCode && x.whCode == first.whCode && x.storageAge >= 61 && x.storageAge <= 90);
                orgInv.Age61_90 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.totalQty) : 0;
                temp = batchInvList.Where(x => x.skuCode == first.skuCode && x.whCode == first.whCode && x.storageAge >= 91 && x.storageAge <= 180);
                orgInv.Age91_180 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.totalQty) : 0;
                temp = batchInvList.Where(x => x.skuCode == first.skuCode && x.whCode == first.whCode && x.storageAge > 181);
                orgInv.Age_GreateThan181 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.totalQty) : 0;
                orgInv.HighAge = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.totalQty) : 0;

                temp = batchInvList.Where(x => x.skuCode == first.skuCode && x.whCode == first.whCode && x.storageAge >= 181 && x.storageAge <= 270);
                orgInv.Age181_270 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.totalQty) : 0;
                temp = batchInvList.Where(x => x.skuCode == first.skuCode && x.whCode == first.whCode && x.storageAge >= 271 && x.storageAge <= 365);
                orgInv.Age271_365 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.totalQty) : 0;
                temp = batchInvList.Where(x => x.skuCode == first.skuCode && x.whCode == first.whCode && x.storageAge > 365);
                orgInv.Age_GreateThan365 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.totalQty) : 0;

                orgInv.ID = orgInv.PrdCode + orgInv.Area + orgInv.Warehouse;
                invList.Add(orgInv);
            }

            #endregion
            return invList;
        }

        public List<Rep_OrgInventory> GetPacklinInv()
        {
            List<Rep_OrgInventory> invList = new List<Rep_OrgInventory>();
            #region 查询数据源
            //FBA账龄表
            this.SqlDb._conStrings[0] = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
            List<PL_InventoryAge.List> StocsAgaList = this.SqlDb.GetListBySql<PL_InventoryAge.List>(@$"SELECT * FROM  SD_PL_InventoryAge");
            List<PL_Inventory.List> StocsList = this.SqlDb.GetListBySql<PL_Inventory.List>(@$"SELECT * FROM  SD_PL_Inventory");
            List<PL_Feeder> feederList = this.SqlDb.GetListBySql<PL_Feeder>(@$"SELECT * FROM SD_PL_Feeder WHERE STATUS='已发出' OR STATUS='已收货'");

            foreach (var item1 in StocsList)
            {
                Rep_OrgInventory orgInv = new Rep_OrgInventory();
                orgInv.PrdCode = item1.sku.Replace("_", ".");
                orgInv.PrdName = "";
                orgInv.StorageOrg = "";
                orgInv.CargoOrg = "";
                orgInv.Area = "美国";
                orgInv.Warehouse = item1.warehouse_code;
                orgInv.WarehouseType = "众包";
                orgInv.Category = "";
                orgInv.InInvQty = item1.actual_quantity + item1.shipping_quantity;
                orgInv.SaleQty = item1.actual_quantity;
                orgInv.ReadyOutQty = 0;
                orgInv.BadQty = 0;
                orgInv.Cost = 0;
                orgInv.Past30IntStock = 0;
                orgInv.Past30OuttStock = 0;
                orgInv.LockQty = item1.shipping_quantity;
                var feederInTransit = feederList.Where(x => x.item_list_sku == item1.sku && x.status == "已发出");
                orgInv.InTransitQty = !feederInTransit.IsNullOrEmpty() ? 0 : feederInTransit.Sum(x => x.item_list_quantity.ToInt());
                var feederInStorage = feederList.Where(x => x.item_list_sku == item1.sku && x.status == "已收货");
                orgInv.InStorageQty = !feederInStorage.IsNullOrEmpty() ? 0 : feederInStorage.Sum(x => x.item_list_quantity.ToInt());

                var temp = StocsAgaList.Where(x => x.Age >= 0 && x.Age <= 30 && x.SKU == item1.sku && x.warehouse_code == item1.warehouse_code);
                orgInv.Age0_30 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.balance_quantity) : 0;
                temp = StocsAgaList.Where(x => x.Age >= 31 && x.Age <= 60 && x.SKU == item1.sku && x.warehouse_code == item1.warehouse_code);
                orgInv.Age31_60 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.balance_quantity) : 0;
                temp = StocsAgaList.Where(x => x.Age >= 61 && x.Age <= 90 && x.SKU == item1.sku && x.warehouse_code == item1.warehouse_code);
                orgInv.Age61_90 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.balance_quantity) : 0;
                temp = StocsAgaList.Where(x => x.Age >= 91 && x.Age <= 180 && x.SKU == item1.sku && x.warehouse_code == item1.warehouse_code);
                orgInv.Age91_180 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.balance_quantity) : 0;
                temp = StocsAgaList.Where(x => x.Age > 181 && x.SKU == item1.sku && x.warehouse_code == item1.warehouse_code);
                orgInv.Age_GreateThan181 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.balance_quantity) : 0;
                orgInv.HighAge = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.balance_quantity) : 0;
                temp = StocsAgaList.Where(x => x.Age >= 181 && x.Age <= 270 && x.SKU == item1.sku && x.warehouse_code == item1.warehouse_code);
                orgInv.Age181_270 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.balance_quantity) : 0;
                temp = StocsAgaList.Where(x => x.Age >= 271 && x.Age <= 365 && x.SKU == item1.sku && x.warehouse_code == item1.warehouse_code);
                orgInv.Age271_365 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.balance_quantity) : 0;
                temp = StocsAgaList.Where(x => x.Age > 365 && x.SKU == item1.sku && x.warehouse_code == item1.warehouse_code);
                orgInv.Age_GreateThan365 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.balance_quantity) : 0;


                orgInv.ID = orgInv.PrdCode + orgInv.Area + orgInv.Warehouse;
                invList.Add(orgInv);
            }

            #endregion
            return invList;
        }
        public List<Rep_OrgInventory> GetOzonInv()
        {
            List<Rep_OrgInventory> invList = new List<Rep_OrgInventory>();
            #region 查询数据源
            //FBA账龄表
            this.SqlDb._conStrings[0] = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
            List<Stocks> StocsList = this.SqlDb.GetListBySql<Stocks>(@$"SELECT * FROM  SD_OZ_Stocks WHERE present>0");
            foreach (var item1 in StocsList)
            {
                Rep_OrgInventory orgInv = new Rep_OrgInventory();
                orgInv.PrdCode = item1.msku;
                orgInv.PrdName = "";
                orgInv.StorageOrg = "";
                orgInv.CargoOrg = "";
                orgInv.Area = "俄罗斯";
                orgInv.Warehouse = item1.type;
                orgInv.WarehouseType = "OZON";
                orgInv.Category = "";
                orgInv.InInvQty = item1.present.ToInt();
                orgInv.SaleQty = item1.present.ToInt();
                orgInv.ReadyOutQty = item1.reserved.ToInt();
                orgInv.BadQty = 0;
                orgInv.Cost = 0;
                orgInv.Past30IntStock = 0;
                orgInv.Past30OuttStock = 0;
                orgInv.LockQty = 0;
                orgInv.InTransitQty = item1.InTransitQty.ToInt();
                orgInv.HighAge = 0;
                orgInv.Age0_30 = 0;
                orgInv.Age31_60 = 0;
                orgInv.Age61_90 = 0;
                orgInv.Age91_180 = 0;
                orgInv.Age181_270 = 0;
                orgInv.Age271_365 = 0;
                orgInv.Age_GreateThan181 = 0;
                orgInv.Age_GreateThan365 = 0;
                orgInv.ID = orgInv.PrdCode + orgInv.Area + orgInv.Warehouse;
                invList.Add(orgInv);
            }

            #endregion
            return invList;
        }

        public List<Rep_OrgInventory> GetWildberriesInv()
        {
            List<Rep_OrgInventory> invList = new List<Rep_OrgInventory>();
            #region 查询数据源
            //FBA账龄表
            this.SqlDb._conStrings[0] = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
            List<WBStocks> StocsList = this.SqlDb.GetListBySql<WBStocks>(@$"SELECT * FROM  SD_WB_WBStocks WHERE stockCount>0");
            foreach (var item1 in StocsList)
            {
                Rep_OrgInventory orgInv = new Rep_OrgInventory();
                orgInv.PrdCode = item1.vendorCode;
                orgInv.PrdName = item1.name;
                orgInv.StorageOrg = "";
                orgInv.CargoOrg = "";
                orgInv.Area = "俄罗斯";
                orgInv.Warehouse = "WB";
                orgInv.WarehouseType = "Wildberries";
                orgInv.Category = "";
                orgInv.InInvQty = item1.stockCount.ToInt();
                orgInv.SaleQty = item1.stockCount.ToInt();
                orgInv.BadQty = 0;
                orgInv.Cost = Convert.ToDecimal(item1.stockSum);
                orgInv.Past30IntStock = 0;
                orgInv.Past30OuttStock = 0;
                orgInv.LockQty = 0;
                orgInv.InTransitQty = 0;
                orgInv.HighAge = 0;
                orgInv.Age0_30 = 0;
                orgInv.Age31_60 = 0;
                orgInv.Age61_90 = 0;
                orgInv.Age91_180 = 0;
                orgInv.Age181_270 = 0;
                orgInv.Age271_365 = 0;
                orgInv.Age_GreateThan181 = 0;
                orgInv.Age271_365 = 0;
                orgInv.ID = orgInv.PrdCode + orgInv.Area + orgInv.Warehouse;
                invList.Add(orgInv);
            }

            #endregion
            return invList;
        }


        public List<Rep_OrgInventory> GetYandexInv()
        {
            List<Rep_OrgInventory> invList = new List<Rep_OrgInventory>();
            #region 查询数据源
            //FBA账龄表
            this.SqlDb._conStrings[0] = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
            List<YDStocks> StocsList = this.SqlDb.GetListBySql<YDStocks>(@$"SELECT * FROM  SD_YD_YDStocks WHERE StockQty>0");
            foreach (var item1 in StocsList)
            {
                Rep_OrgInventory orgInv = new Rep_OrgInventory();
                orgInv.PrdCode = item1.SKU;
                orgInv.PrdName = item1.Name;
                orgInv.StorageOrg = "";
                orgInv.CargoOrg = "";
                orgInv.Area = "俄罗斯";
                orgInv.Warehouse = item1.ShipmentType;
                orgInv.WarehouseType = "Yandex";
                orgInv.Category = "";
                orgInv.InInvQty = item1.StockQty.ToInt();
                orgInv.SaleQty = item1.SaleQty.ToInt();
                orgInv.BadQty = 0;
                orgInv.Cost = 0;
                orgInv.Past30IntStock = 0;
                orgInv.Past30OuttStock = 0;
                orgInv.LockQty = item1.LockQty.ToInt();
                orgInv.InTransitQty = 0;
                orgInv.HighAge = 0;
                orgInv.Age0_30 = 0;
                orgInv.Age31_60 = 0;
                orgInv.Age61_90 = 0;
                orgInv.Age91_180 = 0;
                orgInv.Age181_270 = 0;
                orgInv.Age271_365 = 0;
                orgInv.Age_GreateThan181 = 0;
                orgInv.Age_GreateThan365 = 0;
                orgInv.ID = orgInv.PrdCode + orgInv.Area + orgInv.Warehouse;
                invList.Add(orgInv);
            }

            #endregion
            return invList;
        }


        public List<Rep_OrgInventory> GetREInv()
        {
            List<Rep_OrgInventory> invList = new List<Rep_OrgInventory>();
            #region 查询数据源
            //FBA账龄表
            this.SqlDb._conStrings[0] = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
            List<RE_ProductInventory> StocsList = this.SqlDb.GetListBySql<RE_ProductInventory>(@$"SELECT * FROM  SD_RE_ProductInventory");
            List<RE_ProductInventory.Batch_Info> batchInvList = this.SqlDb.GetListBySql<RE_ProductInventory.Batch_Info>(@$"SELECT * FROM SD_RE_InvBatchInfo");


            foreach (var item1 in StocsList.GroupBy(x => x.product_sku + x.warehouse_code))
            {
                var first = item1.First();
                Rep_OrgInventory orgInv = new Rep_OrgInventory();
                orgInv.PrdCode = first.product_sku;
                orgInv.PrdName = first.product_title;
                orgInv.StorageOrg = "";
                orgInv.CargoOrg = "";
                orgInv.Area = "俄罗斯";
                orgInv.Warehouse = first.warehouse_code;
                orgInv.WarehouseType = "RE仓";
                orgInv.Category = "";
                orgInv.InInvQty = item1.Sum(x => x.sellable + x.reserved + x.unsellable);
                orgInv.SaleQty = item1.Sum(x => x.sellable);
                orgInv.ReadyOutQty = item1.Sum(x => x.reserved);
                orgInv.BadQty = item1.Sum(x => x.unsellable);
                orgInv.Cost = 0;
                orgInv.Past30IntStock = 0;
                orgInv.Past30OuttStock = 0;
                orgInv.LockQty = 0;
                orgInv.InTransitQty = item1.Sum(x => x.onway);
                orgInv.InStorageQty = item1.Sum(x => x.pending);

                var temp = batchInvList.Where(x => x.product_sku == first.product_sku && x.warehouse_code == first.warehouse_code && x.stock_age >= 0 && x.stock_age <= 30);
                orgInv.Age0_30 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.sellable_quantity) : 0;
                temp = batchInvList.Where(x => x.product_sku == first.product_sku && x.warehouse_code == first.warehouse_code && x.stock_age >= 31 && x.stock_age <= 60);
                orgInv.Age31_60 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.sellable_quantity) : 0;
                temp = batchInvList.Where(x => x.product_sku == first.product_sku && x.warehouse_code == first.warehouse_code && x.stock_age >= 61 && x.stock_age <= 90);
                orgInv.Age61_90 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.sellable_quantity) : 0;
                temp = batchInvList.Where(x => x.product_sku == first.product_sku && x.warehouse_code == first.warehouse_code && x.stock_age >= 91 && x.stock_age <= 180);
                orgInv.Age91_180 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.sellable_quantity) : 0;
                temp = batchInvList.Where(x => x.product_sku == first.product_sku && x.warehouse_code == first.warehouse_code && x.stock_age > 181);
                orgInv.Age_GreateThan181 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.sellable_quantity) : 0;
                orgInv.HighAge = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.sellable_quantity) : 0;

                temp = batchInvList.Where(x => x.product_sku == first.product_sku && x.warehouse_code == first.warehouse_code && x.stock_age >= 181 && x.stock_age <= 270);
                orgInv.Age181_270 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.sellable_quantity) : 0;
                temp = batchInvList.Where(x => x.product_sku == first.product_sku && x.warehouse_code == first.warehouse_code && x.stock_age >= 271 && x.stock_age <= 365);
                orgInv.Age271_365 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.sellable_quantity) : 0;
                temp = batchInvList.Where(x => x.product_sku == first.product_sku && x.warehouse_code == first.warehouse_code && x.stock_age > 365);
                orgInv.Age_GreateThan365 = !temp.IsNullOrEmpty() && temp.Count() > 0 ? temp.Sum(x => x.sellable_quantity) : 0;

                orgInv.ID = orgInv.PrdCode + orgInv.Area + orgInv.Warehouse;
                invList.Add(orgInv);
            }

            #endregion
            return invList;
        }


        /// <summary>
        /// 任务
        /// </summary>
        public void Job()
        {
            ExecuteInterface();
        }
    }

}
